package com.bootdo.dj.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.system.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.dj.domain.ModelMenuDO;
import com.bootdo.dj.service.ModelMenuService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author spt
 *
 * @date 2019-01-18 13:29:36
 */
 
@Controller
@RequestMapping("/dj/modelMenu")
public class ModelMenuController  extends BaseController{
	@Autowired
	private ModelMenuService modelMenuService;

	@GetMapping()
	@RequiresPermissions("dj:modelMenu:modelMenu")
	String ModelMenu(){
	    return "dj/modelMenu/modelMenu";
	}
	@RequiresPermissions("sys:menu:menu")
	@RequestMapping("/list")
	@ResponseBody
	List<ModelMenuDO> list(@RequestParam Map<String, Object> params) {
		List<ModelMenuDO> menus = modelMenuService.list(params);
		return menus;
	}
	@ResponseBody
	@GetMapping("/list1")
	@RequiresPermissions("dj:modelMenu:modelMenu1")
	public PageUtils list1(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ModelMenuDO> modelMenuList = modelMenuService.list(query);
		int total = modelMenuService.count(query);
		PageUtils pageUtils = new PageUtils(modelMenuList, total);
		return pageUtils;
	}

	@GetMapping("/add/{pId}")
	@RequiresPermissions("dj:modelMenu:add")
	String add(Model model, @PathVariable("pId") String pId){
		model.addAttribute("pId", pId);
		if (pId.equals("0")) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", modelMenuService.get(pId).getName());
		}
		return "dj/modelMenu/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("dj:modelMenu:edit")
	String edit(@PathVariable("id") String id,Model model){
		ModelMenuDO modelMenu = modelMenuService.get(id);
		String pId = modelMenu.getIdParent();
		model.addAttribute("pId", pId);
		if (pId.equals("0")) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", modelMenuService.get(pId).getName());
		}
		model.addAttribute("modelMenu", modelMenu);
	    return "dj/modelMenu/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("dj:modelMenu:add")
	public R save( ModelMenuDO modelMenu){
		modelMenu.setCreateBy(getUserId());
		modelMenu.setCreateTime(new Date());
		modelMenu.setId(this.get32UUID());
		modelMenu.setDelFlag(0);
		if(modelMenuService.save(modelMenu)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("dj:modelMenu:edit")
	public R update( ModelMenuDO modelMenu){
		modelMenu.setUpdateBy(getUserId());
		modelMenu.setUpdateTime(new Date());
		modelMenuService.update(modelMenu);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("dj:modelMenu:remove")
	public R remove( String id){
		if(modelMenuService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("dj:modelMenu:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		modelMenuService.batchRemove(ids);
		return R.ok();
	}
	
}
